//
//  TF1to16VotingArea.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/13.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TF1to16VotingArea: UIView, UITableViewDelegate, UITableViewDataSource {
    

    //无投票内容
    @objc let votingContentLabel = UILabel()
    @objc let votingListLabel = UILabel()
    //投票内容
    @objc let tableView = UITableView()
    //投票列表
    @objc let questList = NSMutableArray()
    //点击了投票详情  vid
    @objc var voteBlock:TF1to16ExpressionCellmyBlock?
   
    @objc let headerViews = UIView()
    @objc let headerLab = UILabel()
    //投票详情
    @objc let voteDetail = TF1to16VoteDetail()
 
    @objc func backClick()
    {
        self.tableView.isHidden = false
        self.voteDetail.backClick()
       
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.headerViews.addSubview(self.headerLab)

         //TODO:不要下划线
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor(red: 32/255.0, green: 35/255.0, blue: 39/255.0, alpha: 1)
        self.tableView.delegate   = self
        self.tableView.dataSource = self
        self.addSubview(self.tableView)

        //TODO:投票详情
        self.voteDetail.isHidden = true
        self.voteDetail.backgroundColor =  self.tableView.backgroundColor
        self.addSubview(self.voteDetail)
        self.voteDetail.back.addTarget(self, action: #selector(backClick), for: .touchUpInside)
      
         //TODO:投票内容
        self.votingContentLabel.textAlignment = .center
        self.votingContentLabel.numberOfLines = 0
        self.addSubview(self.votingContentLabel)
        
        let votingContentLabelString = NSMutableAttributedString.getAttributed(string: "无投票内容", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0, alpha: 1))
        
        self.votingContentLabel.attributedText = votingContentLabelString
        self.tableView.register(TF1to16VotingAreaCell.self, forCellReuseIdentifier: "TF1to16VotingAreaCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.tableView.frame = self.bounds
        self.voteDetail.frame = self.bounds
        self.votingContentLabel.frame = CGRect(x: 0, y: (self.frame.size.height-25)/2, width: self.frame.size.width, height: 25)

    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.questList.count==0) {
            self.headerViews.isHidden = true
            self.votingContentLabel.isHidden = false
            
            let votingContentLabelString = NSMutableAttributedString.getAttributed(string: "无投票内容", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0 , alpha: 1))
            
            self.votingContentLabel.attributedText = votingContentLabelString
        }else{
           self.headerViews.isHidden = false
            self.votingContentLabel.isHidden = true
        }
        
        return self.questList.count;
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF1to16VotingAreaCell") as! TF1to16VotingAreaCell
        cell.selectionStyle = .none


        cell.tf_Width = self.frame.size.width
        let model =  self.questList[indexPath.row]  as! TFVoteData
        cell.model = model
        
        cell.voteBlock = { [weak self] (vid) in
            guard let `self` = self else { return }
    
            if self.voteBlock != nil {
                self.voteBlock!(vid)
            }
            
        }
       
        
        return cell
    }
    /*
     *  返回每个cell的高度
     */
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 95
       }

    // 选中cell后执行此方法
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print(indexPath.row)
            
            let  model =  self.questList[indexPath.row] as! TFVoteData
          
           
            if self.voteBlock != nil {
                self.voteBlock!(model.vid)
            }
            
        }
    //头部
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        self.headerViews.backgroundColor = UIColor(red: 47/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1)
      
    
        self.headerViews.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)
     

        self.headerLab.frame = CGRect(x: 20, y: 10, width: 68, height: 24)
        self.headerLab.numberOfLines = 0;

         let string = NSMutableAttributedString.getAttributed(string: "投票列表", font: UIFont.PingFang(size: 17), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
        
        self.headerLab.attributedText = string
         

        
        return self.headerViews
    }
    //头部高度
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    
    //投票开始
    @objc func voteNew(_ model:TFVoteData)
    {
        self.questList.insert(model, at: 0)
        self.tableView.reloadData()
     
    }

    //投票结束
    @objc func votePub(_ model:TFVoteData)
    {

        if (self.voteDetail.isHidden == false) {
            
            if (self.voteBlock != nil) {
                self.voteBlock!(model.vid);
            }
     
        }else{
            for i in 0..<self.questList.count {
             let  result =  self.questList[i] as! TFVoteData
          
                
                if ( result.vid == model.vid) {
                    
                     result.info.endTime = self.currentTimeStr()
                    self.tableView.reloadData()
                   
                    return;
                }
            }
            
           
        }
     
    }
    func currentTimeStr()->String
    {
        let date = NSDate.init(timeIntervalSinceNow: 0);//获取当前时间0秒后的时间
        let time = date.timeIntervalSince1970;// *1000 是精确到毫秒，不乘就是精确到秒
        
        let timeString = String.init(format: "%.0f", time)
        return timeString;
    }
    
    //处理当前直播间的所有投票数组
    @objc func allVotes(_ list:NSArray)
    {

        self.questList.removeAllObjects()
        self.questList.addObjects(from: list as! [Any])
        self.tableView.reloadData()
     
    }

    //删除投票
    @objc func delVote(_ model:TFVoteData)

    {
        for i in 0..<self.questList.count {
         let  obj =  self.questList[i] as! TFVoteData
       
            
            if ( obj.vid == model.vid) {
                self.questList.remove(obj)
            
                //清空投票详情
                self.voteDetail.backClick()
                self.tableView.reloadData()
             
                return;
            }
        }
    }
   // #pragma mark 投票成功,更新本地数据  只是记录已投票
    @objc func successfulVote(_ vid:NSString)
    {
        for i in 0..<self.questList.count {
         let  obj =  self.questList[i] as! TFVoteData
   
            if ( obj.vid == vid as String) {
                //已经投票
                obj.voted = true
                self.tableView.reloadData()
               
                return;
            }
        }
    }
    //更新语言
    @objc func upDataLanguage()
    {
        let string = NSMutableAttributedString.getAttributed(string: "投票列表", font: UIFont.PingFang(size: 17), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
        self.headerLab.attributedText = string
    }

  
}
